use jni::{objects::{JObject, JValue}, sys::{jint, jlong}};
use log::debug;
use serde::{Deserialize, Serialize};

use crate::{error::AppErr, utils::{global::get_java_vm, path::login_info_class}};




#[derive(Debug, Serialize)]
pub struct LoginReq {
    pub mac_addr: String
}

#[derive(Debug, Deserialize, Default)]
pub struct LoginRes {
    pub id: i64,
    pub name: String,
    pub mac_addr: String,
    pub location: String,
    pub currency_code: String,
    pub lang: String,
    pub money: u32,
}


impl LoginRes {

    pub fn to_java(&self) -> Result<JObject, AppErr> {
        
        let mut env = get_java_vm().attach_current_thread_as_daemon()?;

        let name = env.new_string(&self.name)?;
        let mac_addr = env.new_string(&self.mac_addr)?;
        let location = env.new_string(&self.location)?;
        let currency_code = env.new_string(&self.currency_code)?;
        let lang = env.new_string(&self.lang)?;

        let obj = env.new_object(
            login_info_class(),
        "(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V", 
        &[
            JValue::Long(self.id as jlong),
            JValue::Object( &name ),
            JValue::Object( &mac_addr ),
            JValue::Object( &location ),
            JValue::Object( &currency_code),
            JValue::Object( &lang),
            JValue::Int( self.money as jint )
        ])?;

        Ok(obj)
    }
}




